<?
$zdir = 1;
include("../includes/functions.php");

if ($_POST[upload] == 1) {
	$fileTitle = fieldToDB($_POST[fileTitle]);
	$fileNotes = fieldToDB($_POST[fileNotes]);
	
	$error = 0;
	$upload = 1;
		
	$someFile     = $_FILES['fileUpload']['tmp_name'];
	$someFileName = $_FILES['fileUpload']['name'];
	$someFileType = $_FILES['fileUpload']['type'];
	$someFileSize = $_FILES['fileUpload']['size'];
		
	// Clean Filename
	$filename = cleanString($someFileName);	
	
	if ( 
		($someFileType == "image/jpeg")  || 
		($someFileType == "image/pjpeg") || 
		($someFileType == "image/gif")   || 
		($someFileType == "image/png")
	   ) {
	   	// We're uploading an image
		$image = 1;		
		$fileDir  = $rootDir.'images/uploads/';
		$filePath = $rootDir.'images/uploads/'.$filename;
	} else { 
		$image = 0; 
		$fileDir  = $rootDir.'documents/';
		$filePath = $rootDir.'documents/'.$filename;
	}
	//chmod($fileDir, 0777);
	echo 'FilePath: '.$filePath;
	if ($error != 1) {
		if(move_uploaded_file($someFile, $filePath)){
			// If we uploaded an image, create thumbnail view
			if ($image == 1) {
				resize_image($filePath,'',100,'thumb',0,0,$someFileType);
			}
			// Add file to database
			$sql = "INSERT into ncdb_Files (
					ItemID,
					ItemType,
					FileTitle,
					FileName,
					FileType,
					FileSize,
					DateUpload
					) values (
					'',
					'',
					'$fileTitle',
					'$filename',
					'$someFileType',
					'$someFileSize',
					'".date('Y-m-d')."'
					)";
				
			$result = mysql_db_query($dbname,$sql);			
		} else {
			$error = 1;
		}
	}	
} else {
	$error = 0;
	$upload = 0;	
}
?>

<html><head>
<script language="javascript">
function uploadComplete(upload,error) {
	if (upload == 1) {
		var progressDiv = window.parent.document.getElementById('uploadStatus');
		var statusDiv   = document.getElementById('uploadStatus2');
		if (error == 1) {
			progressDiv.innerHTML = '';
			statusDiv.innerHTML   = '<img src="../images/iconError.gif"> <?=$errorMessage; ?>';
		} else {
			progressDiv.innerHTML = '';
			statusDiv.innerHTML   = '<img src="../images/iconStatusComplete.gif"> <b>Your Upload Was Successful!</b>';
			
		}
	}
}
function uploadFile(){
	var par = window.parent.document;
	var iframe = par.getElementById('fileFrame');
	var form   = document.getElementById('iform');
			
	var progressDiv = par.getElementById('uploadStatus');
	progressDiv.innerHTML = '<img src="../images/ajaxAnimBar.gif">';
		
	// send
	form.submit();	
}
</script>
<head>
<link rel="STYLESHEET" media="screen" href="<?=$pre; ?>css/<?=$styleSheet; ?>" type="text/css" />	
<style>
body {
	font-size: 68.75%; /* Resets 1em to 11px */
	font-family: Verdana, Arial;
	line-height: 17px;
}
td,p,table,tr,input,select,textarea {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 1em;
}
</style>
<SCRIPT src="<?=$pre; ?>js/jsCommon.js" type="text/javascript"></SCRIPT>
<SCRIPT src="<?=$pre; ?>js/protaculous/prototype.js" type="text/javascript"></SCRIPT>
<script language="JavaScript" type="text/javascript">
function jScriptInit() {
	// Turn on iFrame if necessary
	var par = window.parent.document;
	var iframe = par.getElementById('fileFrame');
	if (iframe.style.display == 'none') {
		iframe.style.display = 'block';
	}
}
</script>
</head>
<body onload="jScriptInit(); uploadComplete(<?=$upload; ?>,<?=$error; ?>);">
	<form name="iform" id="iform" method="post" enctype="multipart/form-data">
	<input type="hidden" name="upload" value="1">
		
	<p><label class="required"><b>Select your file</b></label><br />
	<input type="file" id="fileUpload" name="fileUpload" size="22" /></p>
	
	<p><label class="required"><b>File Title</b></label><br />
	<input type="text" id="fileTitle" name="fileTitle" style="width: 200px;" /></p>
	
	<p><label><b>Notes</b></label><br />
	<textarea id="fileNotes" name="fileNotes" style="width: 200px; height: 50px;"></textarea></p>
	<div style="margin-top: -8px;">
		<div style="margin-right: 20px; float: left;">
			<input type="image" src="<?=$pre; ?>images/buttonUploadSmall.gif" onClick="uploadFile();" alt="Upload File" style="border: none;">
		</div>
		<div style="float: left;" id="uploadStatus2"></div>
		<div style="clear: left;"></div>
	</div>	
	
	</form>
</body>	
</html>